<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/6/22
 * Time: 9:39
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Matchgroup extends Base
{
    /**
     * 帆船级别首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_group')->where('mg_status', 1)->count();// 获取总条数
            $lists = Db::name('match_group')
                ->field('mg_id, mg_name, mg_image, mg_time, mg_gender, mg_year, mg_weight, mg_height, mg_boat_year')
                ->where('mg_status', 1)
                ->where('mg_cate', 1)
                ->page($Nowpage,$limits)
                ->order('mg_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加级别
     * @return mixed|\think\response\Json
     */
    public function groupAdd(){
        if(Request::instance()->isPost()){
            $mg_name = trim(input('mg_name'), ' ');
            $mg_image = input('mg_image');
            $mg_gender = input('mg_gender');
            $mg_year = input('mg_year');
            $mg_weight = input('mg_weight');
            $mg_height = input('mg_height');
            $mg_boat_year = input('mg_boat_year');
            $insert = [
                'mg_name' => $mg_name,
                'mg_image' => $mg_image,
                'mg_gender' => $mg_gender,
                'mg_year' => $mg_year,
                'mg_weight' => $mg_weight,
                'mg_height' => $mg_height,
                'mg_boat_year' => $mg_boat_year,
                'mg_cate' => 1,
                'mg_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_group')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('matchgroup/groupadd');
    }
    
    /**
     * 编辑级别
     * @return mixed|\think\response\Json
     */
    public function groupEdit(){
        $mg_id = input('mg_id');
        if(Request::instance()->isPost()){
            $mg_name = trim(input('mg_name'), ' ');
            $mg_image = input('mg_image');
            $mg_gender = input('mg_gender');
            $mg_year = input('mg_year');
            $mg_weight = input('mg_weight');
            $mg_height = input('mg_height');
            $mg_boat_year = input('mg_boat_year');
            $update = [
                'mg_name' => $mg_name,
                'mg_gender' => $mg_gender,
                'mg_year' => $mg_year,
                'mg_weight' => $mg_weight,
                'mg_height' => $mg_height,
                'mg_boat_year' => $mg_boat_year
            ];
            if(!empty($mg_image)){
                $update['mg_image'] = $mg_image;
            }
            $res = Db::table('up_match_group')->where('mg_id', $mg_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $group = Db::table('up_match_group')
            ->field('mg_name, mg_image, mg_gender, mg_year, mg_weight, mg_height, mg_boat_year')
            ->where('mg_id', $mg_id)
            ->find();
        $this->assign([
            'group' => $group,
            'mg_id' => $mg_id
        ]);
        return $this->fetch('matchgroup/groupedit');
    }
    
    /**
     * 删除级别
     * @return \think\response\Json
     */
    public function groupDel(){
        $mg_id = input('id');
        $res = Db::table('up_match_group')->where('mg_id', $mg_id)->setField('mg_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}